package com.ouheng.shop.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.common.utils.R;
import com.ouheng.shop.domain.RegionDO;
import com.ouheng.shop.service.RegionService;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-08-31 09:53:06
 */
 
@Controller
@RequestMapping("/shop/region")
public class RegionController {
	@Autowired
	private RegionService regionService;
	
	@GetMapping()
	@RequiresPermissions("shop:region:region")
	String Region(){
	    return "shop/region/region";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:region:region")
	public List<RegionDO> list(@RequestParam Map<String, Object> params){
		//查询列表数据
		Map<String, Object> query = new HashMap<>(16);
		List<RegionDO> regionList = regionService.list(query);
		return regionList;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:region:add")
	String add(){
	    return "shop/region/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:region:edit")
	String edit(@PathVariable("id") Long id,Model model){
		RegionDO region = regionService.get(id);
		model.addAttribute("region", region);
	    return "shop/region/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:region:add")
	public R save( RegionDO region){
		if(regionService.save(region)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:region:edit")
	public R update( RegionDO region){
		regionService.update(region);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:region:remove")
	public R remove( Long id){
		if(regionService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:region:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		regionService.batchRemove(ids);
		return R.ok();
	}
	
	@PostMapping(value = "/changeStatus")
	@ResponseBody
	public R changeJobStatus(Long id,String cmd) {
		String label = "禁用";
		if ("start".equals(cmd)) {
			label = "启用";
		} else {
			label = "禁用";
		}
		try {
			regionService.changeStatus(id, cmd);
			return R.ok(label + "成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.ok("任务" + label + "失败");
	}
	
}
